package com.sy.service.finance.impl;

import com.sy.bean.finance.FinanceBill;
import com.sy.common.bean.PageBean;
import com.sy.common.crud.BaseServiceImpl;
import com.sy.mapper.finance.FinanceBillMapper;
import com.sy.service.finance.FinanceBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Service
public class FinanceBillServiceImpl extends BaseServiceImpl<FinanceBill, FinanceBillMapper> implements FinanceBillService {
    @Autowired
    FinanceBillMapper billMapper;

    @Override
    public int save(FinanceBill entity) {
        //根据账单类别获取编码，1：付款单，2：收款单
        String code = getCode(entity.getType());
        entity.setCode(code);

        return super.save(entity);
    }

    /**
     * 获取当天最大付款编码，进行FK年月日模糊查询，并找出最大。
     */
    private String getCode(int type){
        //获取当前付款单最大编码
        String serial = billMapper.getMaxCode(type,getDate());
        serial = serial == null ? "001" : String.format("%03d",Integer.parseInt(serial) + 1);
        return type == 1 ? "FK"+getDate()+serial : "SK"+getDate()+serial;
    }

    /**
     * 获取当前年月日
     */
    private String getDate(){
        LocalDate currentDate = LocalDate.now();
        //确保月和日都为两位数
        return  currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }
}
